package com.mooc.lib_video.ui.home_video

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_network.entity.NetResult
import com.mooc.lib_common.model.HomeFeedEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class VideoViewModel :ViewModel() {

    private var repo: VideoRepo
    val videoData : LiveDataBus<HomeFeedEntity> = LiveDataBus()
    init {
        repo = VideoRepo()
    }

    fun requestFeed(){
        viewModelScope.launch(Dispatchers.IO){
            val entity: NetResult<HomeFeedEntity> =repo.callFeed()
            if (entity is NetResult.Success){
                val homeFeedEntity : HomeFeedEntity = entity.data as HomeFeedEntity
                Log.e("ZXY","homeFeedEntity:"+homeFeedEntity.data!!.size)
                //  item.topComment!!.avatar = item.author!!.avatar
                //        item.topComment!!.name = item.author!!.name
                var intdex = 0
                while (intdex < homeFeedEntity.data!!.size){
                    if (homeFeedEntity.data!![intdex].topComment!=null) {
                        homeFeedEntity.data!![intdex].topComment!!.avatar =
                                homeFeedEntity.data!![intdex]!!.author!!.avatar
                        homeFeedEntity.data!![intdex].topComment!!.name =
                                homeFeedEntity.data!![intdex]!!.author!!.name
                        homeFeedEntity.data!![intdex].comments.add(homeFeedEntity.data!![intdex].topComment!!)
                    }
                    intdex ++
                }
                videoData.postValue(homeFeedEntity)
            }
        }
    }


}